package com.dz.controller.user;

import com.dz.constant.MessageConstant;
import com.dz.constant.RedisConstant;
import com.dz.entity.SetMeal;
import com.dz.redis.RedisCache;
import com.dz.result.Result;
import com.dz.service.SetmealService;
import com.dz.vo.DishItemVO;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 鄧浩奇
 * @classname UserSetMealController
 * @description
 * @since 2024/4/1 16:37
 **/

@Api(tags = "C端-套餐浏览接口")
@RestController
@RequestMapping("/user/setmeal")
public class UserSetMealController {

    private final SetmealService setmealService;

    private final RedisCache redisCache;

    @Autowired
    public UserSetMealController(SetmealService setmealService, RedisCache redisCache) {
        this.setmealService = setmealService;
        this.redisCache = redisCache;
    }

    @ApiOperation("C端-根据分类ID查询套餐")
    @GetMapping("/list")
    public Result<List<SetMeal>> getSetMelListByCategoryId(Long categoryId) {
        Assert.notNull(categoryId, MessageConstant.ID_ERROR);
        String key = RedisConstant.SETMEAL_ + categoryId;

        // 先从Redis中查询
        List<SetMeal> cacheList = redisCache.getCacheList(key);

        if (cacheList.isEmpty()) {
            List<SetMeal> list;
            synchronized (this) {
                cacheList = redisCache.getCacheList(key);
                if (!cacheList.isEmpty()) {
                    return Result.success(cacheList);
                }
                // Redis没有，查询数据库
                list = setmealService.getListByCategoryId(categoryId);
                if (list.isEmpty()) {
                    return Result.error(MessageConstant.RESULT_IS_NULL);
                }
                // 存入Redis,随机设置1到5天的过期时间
                redisCache.setList(key, list, RedisConstant.RANDOM_TIMEOUT, TimeUnit.DAYS);
            }
            return Result.success(list);
        } else return Result.success(cacheList);

    }

    @ApiOperation("根据套餐ID查询包含的菜品")
    @GetMapping("/dish/{id}")
    public Result<List<DishItemVO>> getDishByCategoryID(@PathVariable("id") Long setmealId) {
        Assert.notNull(setmealId, MessageConstant.ID_ERROR);
        String key = RedisConstant.SETMEAL_DISH_ + setmealId;
        // 先从Redis中查询
        List<DishItemVO> cacheList = redisCache.getCacheList(key);
        if (cacheList.isEmpty()) {
            List<DishItemVO> dishItemVOList;
            synchronized (this) {
                cacheList = redisCache.getCacheList(key);
                if (!cacheList.isEmpty()) {
                    return Result.success(cacheList);
                }
                // Redis没有，查询数据库
                dishItemVOList = setmealService.getDishBySetmealId(setmealId);
                if (dishItemVOList.isEmpty()) {
                    return Result.error(MessageConstant.RESULT_IS_NULL);
                }
                // 查询结果存入Redis
                redisCache.setList(key, dishItemVOList, RedisConstant.RANDOM_TIMEOUT, TimeUnit.DAYS);
            }
            return Result.success(dishItemVOList);
        } else return Result.success(cacheList);

    }
}
